//
//  WKPopBGView.swift
//  商家端Swift
//
//  Created by 天下宅 on 16/7/28.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit




class WKPopBaseView: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    var contentView:UIView
    var bgView:UIView

    //contentView的高度
    var thisHeight:CGFloat = 0

    

    
    init() {
        contentView = UIView()
        contentView.backgroundColor = UIColor.white
        bgView = UIView.init(frame:CGRect(x:0, y:0, width:screenWidth, height:screenHeight))
        bgView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        super.init(frame:CGRect(x:0, y:0, width:screenWidth, height:screenHeight))
        
        
        self.addSubview(bgView)
        
        contentView.layer.cornerRadius = 5
        contentView.layer.shadowOpacity = 0.8
        contentView.layer.shadowColor = UIColor.gray.cgColor
        contentView.layer.shadowOffset = CGSize(width:1, height:1)
        self.addSubview(contentView)
        
        
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tap(tap:)))
        bgView.addGestureRecognizer(tap)
        
        contentView.layer.cornerRadius = 5
        contentView.layer.shadowOpacity = 0.8
        contentView.layer.shadowColor = UIColor.gray.cgColor
        contentView.layer.shadowOffset = CGSize(width:1, height:1)
    }
    
    func tap(tap:UITapGestureRecognizer) {
       self.show(flag: false)
    }
    
    
    func finishLayout() {

        
        weak var weakSelf = self
        
        self.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.width.equalTo(280)
            make.center.equalTo(weakSelf!)
            make.height.equalTo(thisHeight)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func show(flag:Bool,view:UIView? = nil){
        
        if flag {
            var thisView = UIApplication.shared.keyWindow! as UIView
            
            if view != nil
            {
                thisView = view!
            }
            
            thisView.addSubview(self)
            
        }
        
        let alpha:CGFloat = flag ? 1 : 0
        
        UIView.animate(withDuration: 0.26, animations: {
            self.alpha = alpha
            self.layoutIfNeeded()
        }) { (finished) in
            if finished{
                if !flag{
                    self.removeFromSuperview()
                }
            }
        }
    }
    

}

//包含title 和 分隔线的 view
class WKPopTitleBaseView: WKPopBaseView {
    
    ///应该再分为子类 这是属于有标题的 但是有一些是属于没有标题的
    /// 标题标签
    lazy var titleLabel:UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFont(ofSize: 16)
        l.textColor = MainColor
        return l
    }()
    //标题下方分割线
    lazy var titleSeparatorLine:UIView = {
        let line = UIView()
        line.backgroundColor = MainColor
        return line
    }()
    
    init(title:String) {
        super.init()
        self.addTitle(withTitle:title )
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addTitle(withTitle title:String) {
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            make.height.equalTo(20)
            make.width.lessThanOrEqualTo(150)
        }
        titleLabel.text = title
        thisHeight += 28
        
        contentView.addSubview(titleSeparatorLine)
        titleSeparatorLine.backgroundColor = MainColor
        titleSeparatorLine.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.left.right.equalTo(contentView)
            make.height.equalTo(0.5)
        }
        
        thisHeight += 6.5
    }
}

//主要针对picker，但是只包含按钮 并没有包含pickerview
class WKPopPickerBaseView: WKPopBaseView {
    var cancleBtn = UIButton.init(type: UIButtonType.custom)
    var submitBtn = UIButton.init(type: UIButtonType.custom)
    
    override init() {
        super.init()
        cancleBtn.setTitle("取消", for: UIControlState.normal)
        submitBtn.setTitle("确定", for: UIControlState.normal)
        submitBtn.backgroundColor = MainColor
        cancleBtn.backgroundColor = MainColor
//        submitBtn.titleLabel?.font = textFont
//        cancleBtn.titleLabel?.font = textFont
        
        
        contentView.addSubview(cancleBtn)
        cancleBtn.snp.makeConstraints { (make) in
            make.left.top.equalTo(contentView).offset(10)
            make.height.equalTo(30)
            make.width.equalTo(60)
        }
        
        contentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(10)
            make.right.equalTo(contentView).offset(-10)
            make.height.equalTo(30)
            make.width.equalTo(60)
        }
        
        thisHeight += 10 + 30
        cancleBtn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
        submitBtn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
        cancleBtn.tag = 100
        submitBtn.tag = 101
        cancleBtn.layer.cornerRadius = 5
        submitBtn.layer.cornerRadius = 5
    }
    
    func btnClick(sender:UIButton){
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
